package com.ryan.demand.controller;


import com.ryan.demand.entity.Demand;
import com.ryan.demand.service.DemandService;
import com.ryan.utils.R;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * <p>
 * 定制需求 前端控制器
 * </p>
 *
 * @author ryan
 * @since 2020-07-30
 */
@RestController
@RequestMapping("/demand")
public class DemandController {

    @Autowired
    private DemandService demandService;

    @PostMapping("/addDemand")
    public R addDemand(@RequestBody Demand demand){
        boolean save = demandService.save(demand);
        return save? R.ok():R.error();
    }

    @PostMapping("/deleteDemand")
    public R deleteBanner(@RequestBody Long[] ids){
        boolean delete = demandService.removeByIds(Arrays.asList(ids));
        return delete?R.ok():R.error();
    }

    @PostMapping("/updateDemand")
    public R updateDemand(@RequestBody Demand demand){
        boolean update = demandService.updateById(demand);
        return update?R.ok():R.error();
    }

    /**
     * 分页+条件查询
     * @param pageIndex
     * @param pageSize
     * @param mobile
     * @return
     */
    @GetMapping("/getAllDemand/{pageIndex}/{pageSize}")
    public R getAllDemand(@PathVariable long pageIndex,
                         @PathVariable long pageSize,
                         @RequestParam(required = false) String mobile){
        Map<String, Object> map = demandService.queryPage(pageIndex, pageSize, mobile);
        return R.ok().data("list", map.get("list")).data("totalPage", map.get("total"));
    }

    @GetMapping("/getDemandById/{id}")
    public R getDemandById(@PathVariable String id){
        Demand demand = demandService.getById(id);
        return R.ok().data("demand", demand);
    }


}

